package views;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import utils.core.CoreUtils;
import utils.system.SystemUtils;

/**
 *
 * @author Juan Carlos García
 */
public class DictionaryCreator extends JFrame{
    
    private String[] columnNames = {"Sintax","yourJava Code"};
    
    /**
     * Constructor of the class
     */
    public DictionaryCreator() {
        //Initialize Components  
        super("yourJava Dictionaries");
        JPanel pane = new JPanel(null);
        GridBagConstraints c = new GridBagConstraints();
        setSize(500,680);
        setLocationRelativeTo(null);
        Object[][] data = CoreUtils.getPropertiesToReplace();

        
        JLabel lblWelcome1 = new JLabel("Welcome to yourJava dictionary creator.");
        JLabel lblWelcome2 = new JLabel("Creates your own Dictionary with your customized code.");
        JLabel lblDictionaryIcon = new JLabel();
        try{
            lblDictionaryIcon.setIcon(new ImageIcon(ImageIO.read(DictionaryCreator.class.getResource("/resources/img/yourJavaDictionariesLogo.png"))));
        }catch(Exception e2){
            JOptionPane.showMessageDialog(null,"Error loading resources");
        }
          
        JLabel lblTitle = new JLabel("Dictionary Title:");
        final JTextField txtTitle = new JTextField();
        JLabel lblVersion = new JLabel("Version:");
        final JTextField txtVersion = new JTextField();
        JLabel lblAuthor = new JLabel("Author:");
        final JTextField txtAuthor = new JTextField();
        final JTable table = new JTable(data,columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        JButton saveButton = new JButton("Create Dictionary");
        
        lblWelcome1.setBounds(20,30,800,25);
        lblWelcome2.setBounds(20,50,800,25);
        lblDictionaryIcon.setBounds(375,10,80,80);
        lblTitle.setBounds(20,100,120,25);
        txtTitle.setBounds(125,100,200,25);
        lblVersion.setBounds(20,125,120,25);
        txtVersion.setBounds(125,125,100,25);
        lblAuthor.setBounds(20,150,120,25);
        txtAuthor.setBounds(125,150,150,25);
        scrollPane.setBounds(20,200,460,400);
        saveButton.setBounds(20,610,460,25);
        
        txtVersion.setText("1");
        txtVersion.setEditable(false);
        
        pane.add(lblWelcome1,null);
        pane.add(lblWelcome2,null);
        pane.add(lblDictionaryIcon,null);
        pane.add(lblTitle,null);
        pane.add(txtTitle,null);
        pane.add(lblVersion,null);
        pane.add(txtVersion,null);
        pane.add(lblAuthor,null);
        pane.add(txtAuthor,null);
        pane.add(scrollPane,null);
        pane.add(saveButton,null);
        
        add(pane);
        setVisible(true);
        setResizable(false);
        
        
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String[][] data = saveDictionaryData(table);
                SystemUtils.createNewDictionary(txtTitle.getText(),txtVersion.getText(),txtAuthor.getText(),data,table.getRowCount(),table.getColumnCount());
            }
        });
    }
    
    private String[][] saveDictionaryData(JTable table){
        String[][] dataSaved = new String[table.getRowCount()][table.getColumnCount()];
        for (int i = 0; i < table.getRowCount(); i++) {
            for (int j = 0; j < table.getColumnCount(); j++) {
                dataSaved[i][j] = (String) table.getValueAt(i,j);
            }
        }    
        return dataSaved;
    }
    
}
